﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

namespace Webtextr
{
    public partial class Page1 : PhoneApplicationPage
    {
        public Page1()
        {
            InitializeComponent();

            StorageHelper helper = new StorageHelper();
            UserSettings settings = helper.LoadSettings();
            this.network.ItemsSource = GetItems();

            if (settings != null)
            {
                username.Text = settings.username;
                password.Password = settings.password;
                if (settings.network == "m")
                {
                    this.network.SelectedIndex = 0;
                }
                else if (settings.network == "o")
                {
                    this.network.SelectedIndex = 1;
                }
                else if (settings.network == "v")
                {
                    this.network.SelectedIndex = 2;
                }
                else if (settings.network == "t")
                {
                    this.network.SelectedIndex = 3;
                }
                else if (settings.network == "em")
                {
                    this.network.SelectedIndex = 4;
                }
                serverUrl.Text = settings.baseUrl;
            }
        }

        private void SaveSettingsClick(object sender, EventArgs e)
        {
            UserSettings settings = new UserSettings();
            settings.username = username.Text;
            settings.password = password.Password;
            if (this.network.SelectedIndex == 0)
            {
                settings.network = "m";
            }
            else if (this.network.SelectedIndex == 1)
            {
                settings.network = "o";
            }
            else if (this.network.SelectedIndex == 2)
            {
                settings.network = "v";
            }
            else if (this.network.SelectedIndex == 3)
            {
                settings.network = "t";
            }
            else if (this.network.SelectedIndex == 4)
            {
                settings.network = "em";
            }
            settings.baseUrl = serverUrl.Text;

            StorageHelper helper = new StorageHelper();
            helper.SaveSettings(settings);

            try
            {
                NavigationService.GoBack();
            }
            catch
            {
                NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.RelativeOrAbsolute));
            }
        }

        private void CancelSettingsClick(object sender, EventArgs e)
        {
            try
            {
                NavigationService.GoBack();
            }
            catch
            {
                NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.RelativeOrAbsolute));
            }
        }

        private IList<string> GetItems()
        {
            List<string> items = new List<string>();
            items.Add("Meteor");
            items.Add("O₂");
            items.Add("Vodafone");
            items.Add("Three");
            items.Add("eMobile");

            return items;

        }
    }
}